package handler

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"logi-hub/cache"
	"logi-hub/model"
	"strconv"
	"time"
)

func RepoAdd(c *gin.Context) {
	name := c.PostForm("name")
	num := c.PostForm("num")
	cope := c.PostForm("cope")
	data := model.Repo{
		Name: name,
		Num:  num,
		Cope: cope,
	}
	err := model.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func OrderAdd(c *gin.Context) {
	orderadder := c.PostForm("orderadder")
	ordername := c.PostForm("ordername")
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	data := model.Order{
		OrderAdder: orderadder,
		OrderName:  ordername,
		Rid:        rid,
	}
	err := model.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func RepoOrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "1"))
	offset := (page - 1) * size
	var data []model.RepoOrder
	/*
		SELECT
		orders.id,
		repos.`name`,
		repos.num,
		repos.cope,
		orders.order_adder,
		orders.order_name
		FROM
		orders
		LEFT JOIN repos ON repos.id=orders.rid
	*/
	redis_key := "RoList_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("orders").
			Joins("LEFT JOIN repos ON repos.id=orders.rid").
			Select(
				"orders.id",
				"repos.`name`",
				"repos.num",
				"repos.cope",
				"orders.order_adder",
				"orders.order_name").
			Offset(offset).Limit(size).Order("orders.id").Find(&data)
		jsonData, _ := json.Marshal(data)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &data)
	}
	var total int64
	model.DB.Table("orders").
		Joins("LEFT JOIN repos ON repos.id=orders.rid").
		Select(
			"orders.id",
			"repos.`name`",
			"repos.num",
			"repos.cope",
			"orders.order_adder",
			"orders.order_name").Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "查询成功",
		"data":  data,
		"total": total,
	})
	return
}
func Detail(c *gin.Context) {
	rid := c.Query("rid")
	var data []model.RepoOrder
	/*
			SELECT
			orders.id,
			repos.`name`,
			repos.num,
			repos.cope,
			orders.order_adder,
			orders.order_name
			FROM
			orders
			LEFT JOIN repos ON repos.id=orders.rid
		orders.rid=2
	*/
	redis_key := "Detail_" + rid
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("orders").
			Joins("LEFT JOIN repos ON repos.id=orders.rid").
			Select(
				"orders.id",
				"repos.`name`",
				"repos.num",
				"repos.cope",
				"orders.order_adder",
				"orders.order_name").
			Where("orders.rid=?", rid).Find(&data)
		jsonData, _ := json.Marshal(data)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &data)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": data,
	})
	return
}
